package com.inspect;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.DirectoryChooser;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
import java.util.stream.Stream;

public class main {
//    public static void main(String[] args) throws Exception {
//        String path = "E:/java/data/2001investigation/C1P2.txt";
//        data data = new data();
//        data.readFile(path, 0);//1表示遍历切割长度为组合值，0表示遍历长度仅包含矩形长宽
//        long startTime = data.startTime;
//        cplex model = new cplex(data);
//        model.runModel(0);
//        long endTime = model.endTime;
//        double seconds = (endTime - startTime)/1000.0;
//        double cost = model.cost;
//        System.out.println(path + "  " + seconds + "  " + cost);
//    }

        public static void main(String[] args) {
        try {
            List<String> fileNames = new ArrayList<>();
            String folderPath = "E:/java/data/test";
            try (java.util.stream.Stream<Path> paths = Files.list(Paths.get(folderPath))) {
                paths.forEach(path -> {
                    if (Files.isRegularFile(path)) {
                        fileNames.add(folderPath + "/" + path.getFileName());
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }

            ArrayList<String> result = new ArrayList<>();
            try {
                for (String path : fileNames) {
                    data data = new data();
                    data.readFile(path, 1);//1表示遍历切割长度为组合值，0表示遍历长度仅包含矩形长宽
                    long startTime = data.startTime;
                    cplex model = new cplex(data);
                    model.runModel(1);
                    long endTime = model.endTime;
                    double seconds = (endTime - startTime) / 1000.0;
                    double cost = model.cost;
//                    System.out.println(path + "  " + seconds + "  " + cost);
                    result.add(path + "  " + seconds + "  " + cost);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            for (String r : result) {
                System.out.println(r);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


//    public static void main(String[] args) {
//        try {
//            List<String> fileNames = new ArrayList<>();
//            String folderPath = "E:/java/data/test";
//            try (java.util.stream.Stream<Path> paths = Files.list(Paths.get(folderPath))) {
//                paths.forEach(path -> {
//                    if (Files.isRegularFile(path)) {
//                        fileNames.add(folderPath + "/" + path.getFileName());
//                    }
//                });
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//            ArrayList<String> result = null;
//            try {
//                result = method(3600L, fileNames);
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//
//            for (String r : result) {
//                System.out.println(r);
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }

    public static ArrayList<String> method(Long timeOut, List<String> fileNames) throws ExecutionException, InterruptedException {
        // 创建线程池
        ExecutorService es = Executors.newFixedThreadPool(4);

        ArrayList<String> fileResults = new ArrayList<>();
        List<Future<String>> futures = new ArrayList<>();
        for (String fileName : fileNames) {
            Callable<String> task = new task(fileName);
            Future<String> future = es.submit(task);
            futures.add(future);
        }
        for (int i = 0; i < futures.size(); i++) {
            Future<String> future = futures.get(i);
            try {
                String result = future.get(timeOut, TimeUnit.SECONDS);//表示等待任务结果的超时时间是 timeOut 秒
                if (result != null) {
                    fileResults.add(result);
                }
            } catch (Exception ex) {
                fileResults.add("求解超时 - " + fileNames.get(i));
            }
        }
        // 关闭线程池
        es.shutdown();
        return fileResults;
    }
}